package br.com.screen;



import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import br.com.core.Conexao;


/**
*
* @author joaopau1o
* @author netobeans
*/

public class ScreenLogin extends JFrame {
	
	JTextField matricula;
	JPasswordField senha;
	JButton entrar;
	Socket socket;
	
	public ScreenLogin() {
		super("ThemTalk");		
		
		JMenuBar menuBar = BarraDeMenu.Instance(); // Chamada para o Singleton BarraDeMenu
		
		//ImageIcon icon = new ImageIcon("/img/icon.png");
		JLabel matricula_ = new JLabel();
		JLabel senha_ = new JLabel();
		matricula = new JTextField();
		senha = new JPasswordField();
		
		matricula_.setText("Matrícula");
		senha_.setText("Senha");
		
		entrar = new JButton();
		entrar.setText("Entrar");
		
		
		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(36, 36, 36)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(matricula_)
                            .addComponent(senha_)
                            .addComponent(matricula)
                            .addComponent(senha, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(entrar)))
                .addContainerGap(45, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(132, Short.MAX_VALUE)
                .addComponent(matricula_)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(matricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(24, 24, 24)
                .addComponent(senha_)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(entrar)
                .addGap(44, 44, 44))
        );
		
        // Evento ao clicar no botão
        entrar.addActionListener(new ActionListener(){  
            public void actionPerformed(ActionEvent e) {  
                if(validaLogin()) {
                	// Apos a validacao do login, inicia a conexao
                	Conexao conexao = new Conexao();
                	if(conexao.conectaServidor()){
                		ScreenContatos contatos = new ScreenContatos();
	                	System.out.println("Bem-vindo ao sistema!");
	                	setVisible(false);
                	}
                } else {
                	JOptionPane.showMessageDialog(null, "Não foi possível efetuar login!\nVerfifique suas informações");
                }
            }  
        });
		
        setJMenuBar(menuBar);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(350, 400);
		setVisible(true);
		
	}
	
	// Método para validar login (incrementar a implementação)
	public boolean validaLogin() {
		if(matricula.getText().equals("12345")) {
			return true;
		} return false;
	}
	
/*	public boolean conectaServidor() {
        try {
        	// Definindo o IP e a porta de conexão com o servidor
        	socket = new Socket("127.0.0.1", 5000);
        	socket.getOutputStream(); // Dados enviados para o servidor
            socket.getInputStream(); // Dados recebidos do servidor 
            return true;
            
        } catch (UnknownHostException ex) {
            
        	JOptionPane.showMessageDialog(null, "Não foi possível conectar.\nVerifique o IP do servidor");
            //Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        	return false;
            
            
        } catch (IOException ex) {
        	
        	JOptionPane.showMessageDialog(null, "Não há conexão com o servidor");
            //Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        	return false;
        }       
    } */
	
	
	public static void main(String[] args) {
		try {
        	// Alterando a skin da UI
    		for(LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        		if ("Nimbus".equals(info.getName())) {
        			UIManager.setLookAndFeel(info.getClassName());
        		}
        	}
        } catch (Exception e) {}
		
		new ScreenLogin();
	}

}
